﻿using System;

namespace Creventive.Wpf.ShellFactory.Services
{
    /// <summary>
    /// Creates objects.
    /// </summary>
    public static class ServiceObjectBuilder
    {
        /// <summary>
        ///   Tries to create an instance of the given type using the content of the given service container for the arguments.
        /// </summary>
        /// <param name = "services">The service container to draw the constructor arguments from.</param>
        /// <param name = "targetType">Type of the target object.</param>
        /// <returns>An object of the target type.</returns>
        public static object Build(this IServiceContainer services, Type targetType)
        {
            var factory = new InjectingServiceFactory(targetType, InstantiationMode.Normal);
            return factory.Create(services);
        }

        /// <summary>
        ///   Tries to create an instance of the given type using the content of the given service container for the arguments.
        /// </summary>
        /// <typeparam name = "T">Type of the target object.</typeparam>
        /// <param name = "services">The services.</param>
        /// <returns>An object of the target type.</returns>
        public static T Build<T>(this IServiceContainer services)
        {
            return (T)Build(services, typeof(T));
        }
    }
}